﻿@page "/ReportViewer"
@page "/ReportViewer/{ReportName?}"
@page "/ReportViewer/{CategoryName?}/{ReportName?}"
@page "/ReportViewer/{ParentCategoryName?}/{CategoryName?}/{ReportName?}"

@inject NavigationManager NavigationManager
@using Microsoft.AspNetCore.WebUtilities
@using BlazorDemo.Pages.Reports.Shared
<link rel="stylesheet" href="_content/BlazorDemo/css/dx-demo-reporting-pages.css">

<DemoPageSectionComponent Id="@GetPageId()" ShowSizeMode="@IsNativeViewer">
    <ChildContentWithParameters Context="Params">
        @if(IsNativeViewer) {
            <NativeReportViewer Params="@Params" ReportName="@ReportName"></NativeReportViewer>
        } else {
            <JSBasedDocumentViewer_Server ReportName="@ReportName"></JSBasedDocumentViewer_Server>
        }
    </ChildContentWithParameters>
    <OptionsContent>
        <div class="w-100 d-flex justify-content-end">
            <OptionComboBox Label="Show in Viewer:"
                            Data="@(ViewerModeData)"
                            Value="@(currentViewer.ToString())"
                            ValueChanged="@((string selectedItem) => { OnViewerChanged(selectedItem); })" />
            <DxButton CssClass="option-component" Text="Show in Report Designer" Click="@NavigateToReportDesigner" />
        </div>
    </OptionsContent>

    @code {
        [Parameter] public string ReportName { get; set; }
        [Parameter] public string CategoryName { get; set; }
        [Parameter] public string ParentCategoryName { get; set; }

        readonly string[] ViewerModeData = new string[] { "Native", "JS Based" };

        string currentViewer;
        const string CurrentViewerName = "CurrentViewer";
        System.Uri uri;
        protected string GetPageId() {
            var id = "";
            if(CategoryName == null || ReportName == null) {
                id = "IndustryReports-Finance-InvoiceReport";
            } else {
                id = CategoryName;
                if(ParentCategoryName != null)
                    id = ParentCategoryName + "-" + id;
                id = id + "-" + ReportName;
            }

            return "Reports-" + id;
        }

        protected void NavigateToReportDesigner() {
            NavigationManager.NavigateTo($"ReportDesigner/{ReportName}");
        }

        protected override void OnParametersSet() {
            uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
            currentViewer = ViewerModeData[0];
            if(QueryHelpers.ParseQuery(uri.Query).TryGetValue(CurrentViewerName, out var currentViewerVal)) {
                currentViewer = currentViewerVal.ToString();
            }
            base.OnParametersSet();
        }
        protected void OnViewerChanged(string value) {
            var uriBuilder = new UriBuilder(uri);
            var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
            q[CurrentViewerName] = value;
            uriBuilder.Query = q.ToString();
            NavigationManager.NavigateTo(uriBuilder.ToString(), true);
        }

        public bool IsNativeViewer {
            get {
                return currentViewer == ViewerModeData[0];
            }
        }
    }
</DemoPageSectionComponent>
